package com.youhome.business.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BRecommendHouseService;
import com.youhome.service.service.ListHubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/11/17.
 */
@RestController
@RequestMapping(value = "/recommend/house")
@Api(tags = "推荐房源")
public class RecommendHouseController {
    @Autowired
    private ListHubService listHubService;
    @Autowired
    private BRecommendHouseService bRecommendHouseService;

    @ApiOperation(value = "添加推荐房源", notes = "添加推荐房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "houseId", required = true, paramType = "query")
    })
    @PostMapping(value = "/create")
    public Object create(String houseId,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bRecommendHouseService.recommendHouse(principal,houseId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "推荐房源列表", notes = "推荐房源列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/list")
    public Object list() throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectRecommendHouse());
    }

    @ApiOperation(value = "删除推荐房源", notes = "删除推荐房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "houseId", required = true, paramType = "query")
    })
    @DeleteMapping(value = "/delete")
    public Object delete(String houseId,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bRecommendHouseService.deleteRecommendHouseByHouseId(principal,houseId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除所有推荐房源", notes = "删除所有推荐房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DeleteMapping(value = "/delete/all")
    public Object deleteAll(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bRecommendHouseService.deleteRecommendHouseByAll(principal);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "推荐房源数量", notes = "推荐房源数量", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/count")
    public Object count(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bRecommendHouseService.selectRecommendHouseCount(principal));
    }
}
